package com.best.oasis.settlement.common.vo.security;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.security.GrantedAuthority;
import org.springframework.security.userdetails.UserDetails;

import com.best.oasis.settlement.common.vo.base.BaseBizVO;

/**
 * It implements Acegi Security's UserDetails interface.
 */
public class UserVO extends BaseBizVO implements Serializable, UserDetails {
    private static final long serialVersionUID = 3832626162173359411L;

    private String            userName;                                       // required

    private String            password;                                       // required

    private String            confirmPassword;

    private String            passwordResetKey;                               //密码重置的key

    private String            name;                                           // required

    private List<RoleVO>      roles            = new ArrayList<RoleVO>();

    private List<RoleGroupVO> roleGroups       = new ArrayList<RoleGroupVO>();

    private boolean           enabled;                                        //是否启用

    private boolean           accountExpired;                                 //账户是否过期

    private boolean           accountLocked;                                  //账户是否锁定

    private boolean           credentialsExpired;                             //账户资格是否过期

    private boolean           ldapUser;                                       //是否Ldap用户
    /**
     * 省
     */
    private String            province;
    /**
     * 城市
     */
    private String            city;
    /**
     * 乡镇
     */
    private String            town;
    /**
     * 街道
     */
    private String            street;
    /**
     * 邮编
     */
    private String            postCode;
    /**
     * 电话区号
     */
    private String            telephoneAreaCode;
    /**
     * 电话号码
     */
    private String            telephoneNumber;
    /**
     * 分机号
     */
    private String            telephoneExtention;
    /**
     * 部门
     */
    private String            department;
    /**
     * 职位
     */
    private String            jobTitle;
    /**
     * email
     */
    private String            email;
    /**
     * 手机号码
     */
    private String            mobile;
    private String            remark;

    /**
     * Default constructor - creates a new instance with no values set.
     */
    public UserVO() {
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getTown() {
        return town;
    }

    public void setTown(String town) {
        this.town = town;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getPostCode() {
        return postCode;
    }

    public void setPostCode(String postCode) {
        this.postCode = postCode;
    }

    public String getTelephoneAreaCode() {
        return telephoneAreaCode;
    }

    public void setTelephoneAreaCode(String telephoneAreaCode) {
        this.telephoneAreaCode = telephoneAreaCode;
    }

    public String getTelephoneNumber() {
        return telephoneNumber;
    }

    public void setTelephoneNumber(String telephoneNumber) {
        this.telephoneNumber = telephoneNumber;
    }

    public String getTelephoneExtention() {
        return telephoneExtention;
    }

    public void setTelephoneExtention(String telephoneExtention) {
        this.telephoneExtention = telephoneExtention;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getJobTitle() {
        return jobTitle;
    }

    public void setJobTitle(String jobTitle) {
        this.jobTitle = jobTitle;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUsername() {
        return userName;
    }

    public String getPassword() {
        return password;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public String getPasswordResetKey() {
        return passwordResetKey;
    }

    public void setPasswordResetKey(String passwordResetKey) {
        this.passwordResetKey = passwordResetKey;
    }

    public List<RoleVO> getRoles() {
        return roles;
    }

    /**
     * Adds a role for the user
     * 
     * @param role the fully instantiated role
     */
    public void addRole(RoleVO role) {
        getRoles().add(role);
    }

    /**
     * @see org.springframework.security.userdetails.UserDetails#getAuthorities()
     * @return GrantedAuthority[] an array of roles.
     */
    public GrantedAuthority[] getAuthorities() {
        return roles.toArray(new GrantedAuthority[0]);
    }

    public boolean isEnabled() {
        return enabled;
    }

    public boolean isAccountExpired() {
        return accountExpired;
    }

    /**
     * @see org.springframework.security.userdetails.UserDetails#isAccountNonExpired()
     */
    public boolean isAccountNonExpired() {
        return !isAccountExpired();
    }

    public boolean isAccountLocked() {
        return accountLocked;
    }

    /**
     * @see org.springframework.security.userdetails.UserDetails#isAccountNonLocked()
     */
    public boolean isAccountNonLocked() {
        return !isAccountLocked();
    }

    public boolean isCredentialsExpired() {
        return credentialsExpired;
    }

    /**
     * @see org.springframework.security.userdetails.UserDetails#isCredentialsNonExpired()
     */
    public boolean isCredentialsNonExpired() {
        return !credentialsExpired;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setRoles(List<RoleVO> roles) {
        this.roles = roles;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public void setAccountExpired(boolean accountExpired) {
        this.accountExpired = accountExpired;
    }

    public void setAccountLocked(boolean accountLocked) {
        this.accountLocked = accountLocked;
    }

    public void setCredentialsExpired(boolean credentialsExpired) {
        this.credentialsExpired = credentialsExpired;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isLdapUser() {
        return ldapUser;
    }

    public void setLdapUser(boolean ldapUser) {
        this.ldapUser = ldapUser;
    }

    /**
     * {@inheritDoc}
     */
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof UserVO)) {
            return false;
        }

        final UserVO user = (UserVO) o;

        return !(userName != null ? !userName.equals(user.getUsername())
                : user.getUsername() != null);

    }

    /**
     * {@inheritDoc}
     */
    public int hashCode() {
        return (userName != null ? userName.hashCode() : 0);
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public List<RoleGroupVO> getRoleGroups() {
        return roleGroups;
    }

    public void setRoleGroups(List<RoleGroupVO> roleGroups) {
        this.roleGroups = roleGroups;
    }

}
